package com.atguigu.spzx.common.handler;

import com.atguigu.spzx.common.handler.exception.SpzxException;
import com.atguigu.spzx.model.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * title:
 * author: zgl
 * date: 2023/12/26
 * description：
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        if(e != null){
            log.error("出现异常：{}", ExceptionUtils.getStackTrace(e));
        }
        return Result.fail();
    }

    @ExceptionHandler(SpzxException.class)
    public Result exception(SpzxException e){
        if(e.getE() != null){
            log.error("出现异常：{}", ExceptionUtils.getStackTrace(e.getE()));
        }
        return Result.fail().code(e.getCode()).message(e.getMessage());
    }
}
